package com.ws.controller;

import com.github.pagehelper.PageInfo;
import com.ws.domain.*;
import com.ws.service.*;
import com.ws.util.PageUtil;
import com.ws.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private AdminsService adminsService;
    @Autowired
    private TypesService typesService;
    @Autowired
    private TopsService topsService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;
    //登录
    public void setAdminsService(AdminsService adminsService) {
        this.adminsService = adminsService;
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session, Model m) {
        //调用service登录查询
        System.out.println(username+password);
        Admins admin = adminsService.login(username, password);
        System.out.println(admin);
        if(admin!=null) {//登录成功

            session.setAttribute("admin", admin);
            session.setAttribute("msg", "登录成功！");
            return "index.jsp";
        }else {
            session.setAttribute("msg", "账号或密码错误，请重新输入！");
            return "login.jsp";
        }
    }
    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        //清空session，跳转到后台登录页面
        session.removeAttribute("admin");
        session.removeAttribute("msg");
        return "login.jsp";
    }

    //后台首页设置
    @RequestMapping("/redirect/{address}")
    public String redirect(@PathVariable String address) {
        return "redirect:/admin/"+address+".jsp";
    }

    //加入今日推荐
    @RequestMapping("/topSave")
    @ResponseBody
    public String topSave(int goodId, int type) {
        return topsService.topSave(goodId, type) ? "ok" : null;
    }

    //移除今日推荐
    @RequestMapping("/topDelete")
    @ResponseBody
    public String topDelete(int goodId, int type) {
        return topsService.topDelete(goodId, type) ? "ok" : null;
    }

    public void setTypesService(TypesService typesService) {
        this.typesService = typesService;
    }

    //查询所有类目
    @RequestMapping("/typeList")
    public String typeList(Model m) {
        //查询所有类目
        List<Type> typeList = typesService.typeList();
        //把查询出来的类目信息存放到Model中
        m.addAttribute("typeList", typeList);
        m.addAttribute("flag",1);
        //跳转到type_list.jsp页面
        return "type_list.jsp";
    }
    //新增类目
    @RequestMapping("/typeSave")
    public String typeSave(Type type) {
        //添加类目后，重新查询所有类目显示
        typesService.typeSave(type);
        return "redirect:typeList";
    }
    //修改类目
    @RequestMapping("/typeEdit")
    public String typeEdit(int id, Model m) {
        //根据ID查询要修改的类目信息
        Type type = typesService.typeEdit(id);
        //把根据ID查询出来的类目信息存放到Model中
        m.addAttribute("type", type);
        return "type_edit.jsp";
    }
    @RequestMapping("/typeUpdate")
    public String typeUpdate(Type type) {
        //修改类目后，重新查询所有类目显示
        typesService.typeUpdate(type);
        return "redirect:typeList";
    }

    //类目删除
    @RequestMapping("/typeDelete")
    public String typeDelete(int id) {
        //删除类目后，重新查询所有类目显示
        typesService.typeDelete(id);
        return "redirect:typeList";
    }

    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

//    商品分页查询
    @RequestMapping("/goodList")
    public String goodList(HttpSession session,
                           @RequestParam(required = false, defaultValue = "0") byte type,
                           @RequestParam(required = false, defaultValue = "1") int page,
                           @RequestParam(required = false, defaultValue = "5") int size) {

        // 设置falg=2，把商品管理高亮选中
        session.setAttribute("flag", 2);
        // 把type存放到作用域中，type=0（默认）代表查询普通商品，type=1代表查询今日推荐
        session.setAttribute("type", type);

        // 分页查询商品1
        if (type == 0) {// 商品管理，分页查询普通商品数据
            // 查询分页所有商品数据
            List<Goods> goodList = goodsService.pageGoods(page, size);
            // 获取分页代码并且发送到浏览器
            PageInfo<Goods> goodsInfo = new PageInfo<>(goodList);
            System.out.println(goodsInfo);
            session.setAttribute("goodsInfo", goodsInfo);
        } else {// 分页查询今日推荐商品
            //查询出今日推荐所有商品的id集合，然后查出当前页的商品集合、分页代码。最后，跳转到good_list.jsp中展示。
            List<Goods> goodList = goodsService.getGoodIdsByType(page, size);//查询今日推进的所有商品的id集合
            PageInfo<Goods> goodsInfo = new PageInfo<>(goodList);
            session.setAttribute("goodsInfo", goodsInfo);
        }
        return "good_list.jsp";
    }

    @RequestMapping("/goodAdd")
    public String goodAdd(Model m) {
        //查询所有类目信息并跳转到添加商品页面显示
        m.addAttribute("typeList", typesService.typeList());
        return "good_add.jsp";
    }
    //添加商品
    @RequestMapping("/goodSave")
    public String goodSave(Goods good, MultipartFile file) throws Exception {
        // 图片上传+获取商品信息保存到数据库
        // 1. 将图片保存到upload文件夹中，并封装文件路径 "../upload/20210610100010752.png"
        good.setCover(UploadUtil.upload(file));
        // 2. 保存商品后，跳转到商品列表界面。
        goodsService.add(good);
        return "redirect:goodList";
    }
    //   处理修改查询请求
    @RequestMapping("/goodEdit")
    public String goodEdit(Model m,Integer id) {
        // 查询所有类目信息并跳转到添加商品页面显示
        m.addAttribute("typeList", typesService.typeList());
        // 查询要修改的商品信息保存到作用域对象中
        m.addAttribute("good", goodsService.getById(id));
        System.out.println(goodsService.getById(id));
        return "good_edit.jsp";
    }

    //处理修改商品请求
    @RequestMapping("/goodUpdate")
    public String goodUpdate(Goods good, MultipartFile file) throws Exception {
        // 图片上传+获取商品信息保存到数据库
        // 1. 将图片保存到upload文件夹中，并封装文件路径 "../upload/20210610100010752.png"
        good.setCover(UploadUtil.upload(file));
        // 2. 修改商品后，跳转到商品列表界面。
        goodsService.update(good);
        return "redirect:goodList";
    }

    //    处理删除商品请求
    @RequestMapping("/goodDelete")
    public String goodDelete(int id) {
        goodsService.goodDelete(id);
        return "redirect:goodList";
    }

    //实现管理员分页查询
    @GetMapping("adminList")
    public String adminList(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                            @RequestParam(value = "size",required = false,defaultValue = "4")int size,HttpSession session){
        List<Admins> list = adminsService.pageAdmin(page, size);
        PageInfo<Admins> pageInfo = new PageInfo<>(list);
        session.setAttribute("pageInfo",pageInfo);
        session.setAttribute("flag",5);
        return "admin_list.jsp";
    }
    //添加管理员
    @RequestMapping("/adminSave")
    public String adminSave(Admins admin) {
        //添加管理员信息后，重新分页查询管理员信息
        adminsService.adminSave(admin);
        return "redirect:adminList";
    }
    //重置管理密码
    @RequestMapping("/adminReset")
    public String adminReset(Admins admin) {
        //重置管理员密码后，重新分页查询管理员信息
        adminsService.adminReset(admin);
        return "redirect:adminList";
    }
    //删除管理员
    @RequestMapping("/adminDelete")
    public String adminDelete(int id) {
        //删除管理员，重新分页查询管理员信息
        adminsService.adminDelete(id);
        return "redirect:adminList";
    }

    //管理客户模块


    //分页查询所有
    @RequestMapping("/userList")
    public String userList(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                           @RequestParam(value = "size",required = false,defaultValue = "4")int size,HttpSession session){
        List<User> userslist = userService.pageUsers(page, size);
        PageInfo<User> pageInfo = new PageInfo<>(userslist);
        session.setAttribute("pageInfo",pageInfo);
        session.setAttribute("flag",4);
        return "user_list.jsp";
    }

    //添加客户
    @RequestMapping("/userSave")
    public String userSave(User user) {
        //添加客户信息后，重新分页查询客户
        userService.userSave(user);
        return "redirect:userList";
    }

    //重置密码
    @RequestMapping("/userReset")
    public String userReset(User user) {
        //重置客户信息后，重新分页查询客户
        userService.userReset(user);
        return "redirect:userList";
    }

    //根据ID查询客户信息后，跳转到修改客户页面
    @RequestMapping("/userEdit")
    public String userEdit(int id, Model m) {
        //根据ID查询客户信息后，跳转到修改客户页面
        m.addAttribute("user", userService.userEdit(id));
        return "user_edit.jsp";
    }

    //修改客户信息后，重新分页查询客户
    @RequestMapping("/userUpdate")
    public String userUpdate(User user) {
        //修改客户信息后，重新分页查询客户
        userService.userUpdate(user);
        return "redirect:userList";
    }

    //删除客户信息后，重新分页查询客户
    @RequestMapping("/userDelete")
    public String userDelete(int id) {
        //删除客户信息后，重新分页查询客户
        userService.userDelete(id);
        return "redirect:userList";
    }

    //分页查询订单
    @RequestMapping("orderList")
    public String orderList(HttpSession session,
                            @RequestParam(value = "status",required = false,defaultValue = "0") Byte status,
                            @RequestParam(value = "page",required = false,defaultValue = "1") int page,
                            @RequestParam(value = "size",required = false,defaultValue = "5")int size){
        //高亮化显示
        session.setAttribute("flag",3);
        session.setAttribute("status",status);
        List<Order> list = ordersService.pageOrder(status, page, size);
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        session.setAttribute("pageInfo",pageInfo);
        return "order_list.jsp";
    }

    /**
     * 订单完成和发货  使用同一个请求地址 本质是更新商品状态
     */
    @RequestMapping("orderUpdate")
    public String orderUpdate(int id,byte status){
        ordersService.orderUpdate(id,status);
        //重新分页查询所有订单
        return "redirect:orderList";
    }

    /**
     * 订单删除 根据id
     * @param id 订单ID
     * @return
     */
    @RequestMapping("orderDelete")
    public String orderDelete(int id){
        ordersService.orderDelete(id);
        return "redirect:orderList";
    }

}